﻿Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports System.ComponentModel
Imports IntoNails.DataAccess
Imports IntoNails.Models
Imports IntoNails.My.Resources

Namespace ViewModel
	Public Class MainWindowViewModel
		Inherits WorkspaceViewModel
		
		#Region "Declarations"
		
		Private ReadOnly _dummy As Object = Nothing
		Private _windowTitle As String
		Private _activeUser As User
		Private _currentPage As WorkspaceViewModel
		Private ReadOnly _userRepository As UserRepository
		
		#End Region
		
		#Region "Constructors"
		
		Public Sub New(ByVal dataSource As String)
            Me.DisplayName = StringResources.INTONAILS_LIGHT_ASSEMBLY_NAME
			
			_userRepository = New UserRepository(dataSource)
			
			Me.LoadWorkspaces()
		End Sub
		
		#End Region
		
		#Region "Properties"
		
		Public Property WindowTitle() As String
			Get
				Return _windowTitle
			End Get
			Private Set(ByVal value As String)
				_windowTitle = value
			End Set
		End Property
		
		''' <summary>
		''' Gets the user with authority, who controls the program
		''' </summary>
		Public Property ActiveUser() As User
			Get
				Return _activeUser
			End Get
			Private Set(ByVal value As User)
				If ReferenceEquals(_activeUser, value) Then
					Return
				End If
				_activeUser = value
				
				Me.OnPropertyChanged("ActiveUser")
			End Set
		End Property
		
		''' <summary>
		''' Gets/sets the current workspace.
		''' </summary>
		Public Property CurrentWorkspace() As WorkspaceViewModel
			Get
				Return _currentPage
			End Get
			Set(ByVal value As WorkspaceViewModel)
				'If _currentPage IsNot Nothing Then
				'    _currentPage.IsCurrent = False
				'End If
				
				_currentPage = value
				
				If _currentPage IsNot Nothing Then
					_currentPage.IsCurrent = True
				End If
				
				Me.OnPropertyChanged("CurrentWorkspace")
			End Set
		End Property
		
		#End Region
		
		#Region "Commands"
		
		#End Region
		
		#Region "Workspaces"
		
		''' <summary>
		''' If the program is first-load then shows the admin registration,
		''' else shows the login workspace.
		''' </summary>
		Private Sub LoadWorkspaces()
			' If we have had the user-datafile, load the login-view
			If IO.File.Exists(_userRepository.DataFile) Then
				Me.LoadLoginWorkspace()
			Else 'load the admin-registration view
				Me.LoadAdminRegistrationWorkspace()
			End If
		End Sub
		
		Private Sub LoadIntoNailsWorkspace()
			Dim intonails As New IntoNailsViewModel(Me.ActiveUser)
			intonails.Parent = Me
			AddHandler intonails.UserLogout, AddressOf Me.IntoNails_UserLogout
			Me.CurrentWorkspace = intonails
		End Sub
		
		Private Sub LoadLoginWorkspace()
			Dim login As New LoginViewModel(_userRepository)
			login.Parent = Me
			AddHandler login.LoginSucceed, AddressOf Me.LoginWorkspace_Succeed
			AddHandler login.LoginCancelled, AddressOf Me.LoginWorkspace_Cancelled
			Me.CurrentWorkspace = login
		End Sub
		
		Private Sub LoadAdminRegistrationWorkspace()
			Dim admin As New AdminRegistrationViewModel()
			admin.Parent = Me
			AddHandler admin.RegistrationCompleted, AddressOf Me.AdminWorkspace_RegistrationCompleted
			AddHandler admin.RegistrationCancelled, AddressOf Me.AdminWorkspace_RegistrationCancelled
			Me.CurrentWorkspace = admin
		End Sub
		
		#End Region
		
		#Region "Workspaces Handlers"
		
		Private Sub AdminWorkspace_RegistrationCompleted(ByVal sender As Object, ByVal e As UserEventArgs)
			Me.ActiveUser = e.UserProfile
			
			Dim adminWorkspace As AdminRegistrationViewModel = DirectCast(sender, AdminRegistrationViewModel)
			' closes the admin-workspace...
			adminWorkspace.Dispose()
			
			' shows the intonails-workspace after the registtration completed...
			Me.LoadIntoNailsWorkspace()
		End Sub
		
		Private Sub AdminWorkspace_RegistrationCancelled(ByVal sender As Object, ByVal e As EventArgs)
			Me.CloseCommand.Execute(Nothing)
		End Sub
		
		Private Sub LoginWorkspace_Succeed(ByVal sender As Object, ByVal e As UserLoginEventArgs)
			Me.ActiveUser = e.UserProfile
			
			Dim loginWorkspace As LoginViewModel = DirectCast(sender, LoginViewModel)
			' closes the login-workspace...
			loginWorkspace.Dispose()
			
			' shows the intonails-workspace after the registtration succeed...
			Me.LoadIntoNailsWorkspace()
		End Sub
		
		Private Sub LoginWorkspace_Cancelled(ByVal sender As Object, ByVal e As EventArgs)
			Me.CloseCommand.Execute(Nothing)
		End Sub
		
		Private Sub IntoNails_UserLogout(ByVal sender As Object, ByVal e As EventArgs)
			Dim intonailsWorkspace As IntoNailsViewModel = DirectCast(sender, IntoNailsViewModel)
			' closes the intonails-workspace...
			intonailsWorkspace.Dispose()
			
			' resets the active-user...
			Me.ActiveUser = CType(_dummy, User)
			
			' loads the login-workspace...
			Me.LoadWorkspaces() 'just in case someone deleted the datafile and logout...
		End Sub
		
#End Region

        Protected Overrides Sub OnDispose()
            MyBase.OnDispose()

            Me.ActiveUser = Nothing

            If Me.CurrentWorkspace IsNot Nothing Then
                Me.CurrentWorkspace.Dispose()
            End If
        End Sub
		
	End Class
End Namespace
